package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.Record;
import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.RecordMapper;
import com.shaw.workrecord.pojo.request.RecordAddRequest;
import com.shaw.workrecord.pojo.request.RecordEditRequest;
import com.shaw.workrecord.pojo.request.RecordPageRequest;
import com.shaw.workrecord.pojo.response.RecordResponse;
import com.shaw.workrecord.service.IRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Override
    public PageResponse<RecordResponse> getRecordPage(RecordPageRequest request) {
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(Record::getGroupId, request.getGroupId());
        Page<Record> page = new Page<>(request.getPageIndex(), request.getPageSize());

        Page<Record> result = baseMapper.selectPage(page, wrapper);
        List<RecordResponse> collect = result.getRecords().stream().map(i -> {
            return BeanUtil.copyProperties(i, RecordResponse.class);
        }).collect(Collectors.toList());
        PageResponse<RecordResponse> recordResponsePage = new PageResponse<>();
        recordResponsePage.setRows(CollectionUtil.isEmpty(collect) ? new ArrayList<>() : collect);
        recordResponsePage.setTotal(result.getTotal());
        return recordResponsePage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRecord(RecordAddRequest request) {
        Record record = BeanUtil.copyProperties(request, Record.class);
        return save(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editRecord(RecordEditRequest request) {
        Record record = BeanUtil.copyProperties(request, Record.class);
        return saveOrUpdate(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRecord(Integer id) {
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Record::getId).eq(Record::getId, id);
        Record record = baseMapper.selectOne(wrapper);
        if (record == null) {
            throw new CustomerException("不存在记录");
        }
        return removeById(id);
    }

    @Override
    public RecordResponse getRecordDetail(Integer id) {
        Record record = baseMapper.selectById(id);
        return BeanUtil.copyProperties(record, RecordResponse.class);
    }
}
